<?php


namespace app\index\model;


use think\Model;

class PostComment extends Model
{
    protected $name = 'admin_post_comment';

    # 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 评论列表
     * @param $post_id
     * @param int $parent_id
     * @param string $order
     * @param $page
     * @param null $pageSize
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCommentList($post_id, $parent_id = 0, $order = 'desc', $page, $pageSize = null)
    {
        $data = self::where(['post_id' => $post_id, 'parent_id' => $parent_id, 'status' => 1])
            ->field(['id', 'parent_id', 'post_id', 'reply_id', 'uid', 'content', 'create_time'])
            ->order('create_time', $order)
            ->page($page, $pageSize)
            ->select();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    /**
     * 评论总数
     * @param $post_id
     * @param $parent_id
     * @return int
     */
    public static function getCommentCount($post_id, $parent_id)
    {
        $count = self::where(['post_id' => $post_id, 'parent_id' => $parent_id, 'status' => 1])->count();
        return $count;
    }
}